package com.cake.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cake.base.config.Constant;
import com.cake.base.util.Base64FileUtils;
import com.cake.base.util.PropertiesUtils;
import com.cake.base.util.ReturnUtils;
import com.cake.entity.XxProduct;
import com.cake.entity.XxProductCategory;
import com.cake.service.inter.IXxCouponService;
import com.cake.service.inter.IXxProductCategoryService;
import com.cake.service.inter.IXxProductService;

/**
 * 商品分类控制层
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "productCategory")
public class ProductCategoryController {

	@Autowired
	private IXxProductCategoryService xxProductCategoryService;
	
	@Autowired
	private IXxProductService xxProductService;
	
	@Autowired
	private IXxCouponService xxCouponService;
	
	/**
	 * 查询所有的商品分类
	 * @return
	 * @throws Exception 
	 */
	@GetMapping(value = "selectProductCategorys")
	public Object selectProductCategorys() throws Exception{
		return ReturnUtils.forNoramlSuccess(xxProductCategoryService.selectByAttributeAnd(new XxProductCategory()));
	}
	
	/**
	 * 查询所有的商品分类
	 * @return
	 * @throws Exception 
	 */
	@GetMapping(value = "selectProductCategorysForInsertCoupon")
	public Object selectProductCategorysForInsertCoupon() throws Exception{
		XxProductCategory xxProductCategory = new XxProductCategory();
		xxProductCategory.setOrders(0);
		return ReturnUtils.forNoramlSuccess(xxProductCategoryService.selectByAttributeAnd(xxProductCategory));
	}
	
	/**
	 * 根据id查询商品分类详情
	 * @return
	 * @throws Exception 
	 */
	@GetMapping(value = "selectProductCategoryById")
	public Object selectProductCategoryById(Long productCategoryId) throws Exception{
		return ReturnUtils.forNoramlSuccess(xxProductCategoryService.selectByPrimaryKey(productCategoryId));
	}

	/**
	 * 添加一条商品分类
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "insertProductCategory")
	public Object insertProductCategory(@RequestBody XxProductCategory xxProductCategory) throws Exception{
		/**
		 * 分类图片处理
		 */
		String imageUrl = Base64FileUtils.base64ToImageFile(xxProductCategory.getBase64String(), 
				PropertiesUtils.getProperties(Constant.SERVER_FILE_IMAGE_PHYSICAL_URL_PRE), 
				UUID.randomUUID().toString() + ".png");
		xxProductCategory.setCategoryImg(imageUrl);
		
		xxProductCategory.setCreateDate(new Date());
		xxProductCategory.setOrders(0);
		xxProductCategoryService.insertSelective(xxProductCategory);
		return ReturnUtils.forNoraml("000000", "操作成功");
	}
	
	/**
	 * 修改一条商品分类
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "updateProductCategory")
	public Object updateProductCategory(@RequestBody XxProductCategory xxProductCategory) throws Exception{
		if(xxProductCategory.getIsNew() < 0){
			String imageUrl = Base64FileUtils.base64ToImageFile(xxProductCategory.getBase64String() 
					, PropertiesUtils.getProperties(Constant.SERVER_FILE_IMAGE_PHYSICAL_URL_PRE), 
					UUID.randomUUID().toString() + ".png");
			xxProductCategory.setCategoryImg(imageUrl);
		}
		xxProductCategoryService.updateByPrimaryKeySelective(xxProductCategory);
		return ReturnUtils.forNoraml("000000", "操作成功");
	}
	
	/**
	 * 检查商品分类是否可以禁用
	 * @return
	 */
	@GetMapping(value = "checkProductCategoryCanForbidden")
	public Object checkProductCategoryCanForbidden(Long productCategoryId){
		if(xxCouponService.selectCouponByProductCategory(productCategoryId) > 0){
			return ReturnUtils.forNoramlError("500", "111111", "当前分类还有与之对应的可使用优惠券，请先解除该分类，再禁用");
		}else{
			return ReturnUtils.forNoraml("000000", "操作成功");
		}
	}
	
	/**
	 * 修改一条商品分类状态
	 * 并修改分类下所有的商品状态
	 * @return
	 * @throws Exception 
	 */
	@GetMapping(value = "forbiddenProductCategory")
	public Object forbiddenProductCategory(Long productCategoryId, Integer orders) throws Exception{
		XxProductCategory xxProductCategory = xxProductCategoryService.selectByPrimaryKey(productCategoryId);
		if(xxProductCategory != null){
			xxProductCategory.setOrders(orders);
			int updateCode = xxProductCategoryService.updateByPrimaryKeySelective(xxProductCategory);
			if(updateCode > 0){
				List<XxProduct> list = xxProductService.selectProductsByCategory(productCategoryId);
				for (XxProduct xxProduct : list) {
					xxProduct.setIsMarketable(orders.equals(0));
					xxProductService.updateByPrimaryKeySelective(xxProduct);
				}
			}
		}
		return ReturnUtils.forNoraml("000000", "操作成功");
	}
}